#include "brepWidget.h"
#include <qmessagebox.h>

brepWidget::brepWidget(modelSoft* myModelSoft, QWidget* parent)
	:QWidget(parent)
	, myModelSoft(myModelSoft)
	, ui(new Ui::brepWidgetClass())
{
	ui->setupUi(this);
	ui->booleanSelect_3->addItem("和运算");
	ui->booleanSelect_3->addItem("差运算");
	ui->booleanSelect_3->addItem("交运算");
	ui->confirmButton_3->setEnabled(false);
	ui->modifyButton_3->setEnabled(false);
	myModelSoft->myView->getInteractorStyle()->setBrepOperation(true);

	//实例化特征
	brepFeature = new BrepFeature();
	brepFeature->setBrepId(myModelSoft->shapeTag++);
	CreateConnections();
}

brepWidget::~brepWidget()
{
	delete ui;
}

void brepWidget::CreateConnections()
{
	connect(myModelSoft->currShapeSelector, &OccShapeSelector::sentShapePipeline, this, &brepWidget::receiveShapePipeline);
	connect(ui->cancelButton_3, &QPushButton::clicked, this, &brepWidget::close);
	connect(ui->confirmButton_3, &QPushButton::clicked, this, &brepWidget::OnConfirm);
	connect(ui->modifyButton_3, &QPushButton::clicked, this, &brepWidget::OnModify);
	connect(ui->targetCancelSelect, &QPushButton::clicked, this, &brepWidget::OnCancelSelTarget);
	connect(ui->toolCancelSelect, &QPushButton::clicked, this, &brepWidget::OnCancelSelTool);
	connect(ui->targetSelect, &QPushButton::clicked, this, &brepWidget::OnSelTarget);
	connect(ui->toolSelect, &QPushButton::clicked, this, &brepWidget::OnSelTool);
	connect(ui->booleanSelect_3, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &brepWidget::OnBoolOPChange);
}

void brepWidget::receiveShapePipeline(Handle(QIVtkSelectionPipeline) pipeline)
{
	//获取选择pipeline的特征
	int pipelineId = pipeline->GetShapeID();
	auto iter = myModelSoft->m_Features.find(pipelineId);
	if (iter == myModelSoft->m_Features.end()) {
		return;
	}
	BaseFeature* feature = iter->second;
	if (currSelectTag == 1 && !targetReceive) {
		m_selectedPipelineTarget = pipeline;
		brepFeature->setTargetFeature(feature);
		if (!m_selectedPipelineTool.IsNull())
		{
			ui->modifyButton_3->setEnabled(true);
		}
		ui->targetSelSubWidget->setStyleSheet("background-color:rgb(255,178,102)");
		targetReceive = true;
	}
	else if(currSelectTag == 0 && !toolReceive) {
		m_selectedPipelineTool = pipeline;
		brepFeature->setToolFeature(feature);
		ui->modifyButton_3->setEnabled(true);
		ui->toolSelSubWidget->setStyleSheet("background-color:rgb(255,178,102)");
		toolReceive = true;
	}
}

void brepWidget::closeEvent(QCloseEvent* event)
{
	// 清除已选择的Actor
	myModelSoft->myView->getInteractorStyle()->clearCurrSelector();
	myModelSoft->myView->getInteractorStyle()->setBrepOperation(false);
	// 没有点确定，删除创建的Actor
	if (!confirmCreated)
	{
		if (!m_selectedPipelineTarget.IsNull())
		{
			m_selectedPipelineTarget->SetPipelineVisibility(true);
		}
		if (!m_selectedPipelineTool.IsNull())
		{
			m_selectedPipelineTool->SetPipelineVisibility(true);
		}
		if (!m_resultPipeline.IsNull())
		{
			myModelSoft->RemovePipelineFromRenderer(m_resultPipeline);
			myModelSoft->myView->getRenderWindow()->Render();
		}
	}
}

void brepWidget::OnConfirm()
{
	
	QStandardItem* item1 = new QStandardItem(QString("布尔运算-%1").arg(brepFeature->getBrepId()));
	int row = myModelSoft->ui->model->rowCount();
	item1->setEditable(false);
	myModelSoft->ui->model->setItem(row, 0, item1);
	QStandardItem* item2 = new QStandardItem(QString("隐藏"));
	item2->setCheckable(true);
	myModelSoft->ui->model->setItem(row, 1, item2);
	myModelSoft->m_Features.insert(std::pair<Standard_Integer, BrepFeature*>(brepFeature->getBrepId(), brepFeature));
	myModelSoft->RemovePipelineFromRenderer(m_selectedPipelineTarget);
	myModelSoft->RemovePipelineFromRenderer(m_selectedPipelineTool);
	//目标体和工具体记录参与的布尔运算操作
	brepFeature->AddOperation(brepFeature);

	confirmCreated = true;

	close();
}

void brepWidget::OnModify()
{
	if (modifyStatus) //查看是否已修改过
	{
		return;
	}
	auto shapetargetId = m_selectedPipelineTarget->GetShapeID();
	auto shapetoolId = m_selectedPipelineTool->GetShapeID();
	auto shape2Type = m_selectedPipelineTool->getShape();
	auto shape1Type = m_selectedPipelineTarget->getShape();
	
	TopoDS_Shape resultShape;
	myModelSoft->BrepOperation(resultShape,m_selectedPipelineTarget->getShape(), m_selectedPipelineTool->getShape(), brepOpMode);
	if (resultShape.IsNull())
	{
		QMessageBox::warning(this, "Warning", "NUll operation result!!!");
	}

	modifyStatus = true;
	ui->confirmButton_3->setEnabled(true);
	//指定resultPipeline的id和BrepId一致确保如果选择的是布尔运算后的模型找到特征是BrepFeature
	m_resultPipeline = myModelSoft->CreatePipeline(resultShape, this->brepFeature->getBrepId(),true);
	
	// 暂时隐藏运算的Actor
	m_selectedPipelineTarget->SetPipelineVisibility(false);
	m_selectedPipelineTool->SetPipelineVisibility(false);
	brepFeature->setResultId(m_resultPipeline->GetShapeID());
	brepFeature->setShapeId(m_resultPipeline->GetShapeID());
	myModelSoft->myView->getRenderWindow()->Render();
}

void brepWidget::OnSelTarget()
{
	currSelectTag = 1;
	myModelSoft->myView->getInteractorStyle()->setSelectionMode(SM_Solid);
	myModelSoft->changeSelector(SM_Solid);
	myModelSoft->myView->getInteractorStyle()->setCurrSelector(myModelSoft->currSelector);

}

void brepWidget::OnSelTool()
{
	currSelectTag = 0;
	myModelSoft->myView->getInteractorStyle()->setSelectionMode(SM_Solid);
	myModelSoft->changeSelector(SM_Solid);
	myModelSoft->myView->getInteractorStyle()->setCurrSelector(myModelSoft->currSelector);

}

void brepWidget::OnCancelSelTarget()
{
	// 已经修改不能取消选中
	if (!m_selectedPipelineTarget.IsNull() && !modifyStatus)
	{
		myModelSoft->RemoveSelectedActor(m_selectedPipelineTarget);
		m_selectedPipelineTarget = nullptr;
		ui->targetSelSubWidget->setStyleSheet("background-color:rgb(255,255,255)");
		ui->modifyButton_3->setEnabled(false);
		targetReceive = false;
	}
}

void brepWidget::OnCancelSelTool()
{
	// 已经修改不能取消选中
	if (!m_selectedPipelineTool.IsNull() && !modifyStatus)
	{
		myModelSoft->RemoveSelectedActor(m_selectedPipelineTool);
		m_selectedPipelineTool = nullptr;
		ui->toolSelSubWidget->setStyleSheet("background-color:rgb(255,255,255)");
		ui->modifyButton_3->setEnabled(false);
		toolReceive = false;
	}
}

void brepWidget::OnBoolOPChange(int index)
{
	switch (index)
	{
	case 0:
		brepOpMode = 0;
		brepFeature->setBrepType(0);
		break;
	case 1:
		brepOpMode = 1;
		brepFeature->setBrepType(1);
		break;
	case 2:
		brepOpMode = 2;
		brepFeature->setBrepType(2);
		break;
	}
}
